package com.yzjs.localhost.service.hrmCertificate.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmCertificate.HrmCertificateMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmCertificate.HrmCertificateEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmCertificate.HrmCertificateService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmCertificateService")
public class HrmCertificateServiceImpl extends BaseServiceImpl<HrmCertificateEntity, HrmCertificateMapper > implements HrmCertificateService {


    @Override
    public ComparisonResultDTO CertificateCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        String beforceInfo = beforce.getInfo();
        Map<String, Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (beforce.getOperatorCode().contains("add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 证书类型，发证机关，用户ID，证书编号，证书名称，操作人，签发人
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "certType,issuingAuthority,certUser,certNum,certName,create_by,cert_signer");
        } else if (beforce.getOperatorCode().contains("edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 证书类型，发证机关，用户ID，证书编号，证书名称，操作人，签发人
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "certType,issuingAuthority,certUser,certNum,certName,create_by,cert_signer");
        } else if (beforce.getOperatorCode().contains("remove")) {
            beforceParse.put("deleteFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户ID
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "certIds,deleteFlag");
            Object kycgId = afterParse.get("certIds");
            if (kycgId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("certIds is null");
                resultDTO.setMathFlag(false);
                return  resultDTO;
            }
            HrmCertificateEntity hrmCertificateEntity = new HrmCertificateEntity();
            hrmCertificateEntity.setCertId(Long.valueOf((String) afterParse.get("certIds")));
            hrmCertificateEntity.setDeleteFlag((String) afterParse.get("deleteFlag"));
            hrmCertificateEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmCertificateEntity);
            return resultDTO;
        }

        // 记录表表信息
        HrmCertificateEntity hrmCertificateEntity = JsonUtil.json2pojo(after.getInfo(), HrmCertificateEntity.class);
        hrmCertificateEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmCertificateEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmCertificateEntity);

        return resultDTO;
    }
}